<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% String contextPath = request.getContextPath(); %>
<%
	String msg= (String)request.getAttribute("msg");
	String start_date =(String)request.getAttribute("start_date");
	String last_date =(String)request.getAttribute("last_date");
	
%>
<meta http-equiv="content-script-type" content="text/javascript">
<%@ page import="common.*"%>
<%@ page import="java.util.*" %>
<%@ page import="db.*" %>
<%@ page import ="java.text.NumberFormat" %>
<%@ page import="java.sql.*" %>
<%
	String customer_code =(String)request.getAttribute("customer_code");
	Customer cs = Customer.find_by_code(customer_code);
%>
<%
	int s = 0;
	List<Order> orderlist = new ArrayList<Order>();
//	System.out.println(orderlist.size());
	int j=0;
	int items=0;
	int max = (orderlist.size() + 1);
	NumberFormat nfNum = NumberFormat.getNumberInstance();

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<link href="<%= contextPath %>/css/order.css" rel="stylesheet" type="text/css">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
			<style type="text/css">  
				<!-- 
				div.hidden {
					display: none;
				}
				-->  
			</style>  
		<script type="text/javascript">
			function showButton(id) {
				var e=document.getElementById(id);
				e.className="";
			}
		</script>
		<title>受注実績一覧</title>
	</head>
	
	<body id="orderlist">
		<div>
			<p>顧客名：<%
									if(cs!=null){%>
										<%=cs.getName()%>
									<%}
									%></p>
			<p>顧客コード：<%if(customer_code!=null){ %>
			<%=request.getAttribute("customer_code") %>
			<%} %></p>
			<p>対象期間：<%=request.getAttribute("start_date") %>
									～<%=request.getAttribute("last_date") %></p>
		</div>
			<%--= //msg.replaceAll("\n", "<br />") --%>
			<%--= start_date.replaceAll("/", "-") --%>
			<%--= last_date.replaceAll("/", "-") --%>
			<%	Connection con = null;
					Statement stm = null;
					ResultSet rs = null;

					try {
						con = DBManager.getConnection();
						stm = con.createStatement();
						String sql = "select item_code ,item.name ,size ,order_item.price ,sum(orders) as order_total ,"
								+ " (order_item.price*sum(orders)) as total_price, order_info.date,  order_info.judge, order_info.customer_code "
								+ " from order_item "
								+ " left join item "
								+ " on order_item.item_code = item.code " 
								+ " left join order_info "
								+ " on order_item.order_num = order_info.num "
								+ " group by item_code , order_item.price " 
								+ " having order_info.date between '"
								+   start_date + "'  and '" +  last_date + "'"
								+ " and order_info.judge = '1'" 
								+ " and order_info.customer_code like'%" + customer_code + "%' ";
								rs = stm.executeQuery(sql);

					%>
			<table>
				<tr>
						<th width="150" height="23">商品コード</th>
						<th width="450">商品名</th>
						<th width="150">規格</th>
						<th width="150">単価(円)</th>
						<th width="150">注文数(個)</th>
						<th width="200">合計金額(円)</th>
				</tr>
					<%
					int orderAll = 0;
					int priceAll = 0;
				while(rs.next()) {
					orderAll += rs.getInt("order_total");
					priceAll += rs.getInt("total_price");
					%>
					<tr>
						<td width="300" height="30"><%=rs.getString("item_code") %></td>
						<td width="1300"><%=rs.getString("name") %></td>
						<td width="300"><%=rs.getString("size") %></td>
						<td width="400"><%=nfNum.format(rs.getInt("price"))%></td>
						<td width="400"><%=nfNum.format(rs.getInt("order_total"))%></td>
						<td width="450"><%=nfNum.format(rs.getInt("total_price"))%></td>
					</tr>
				<%	}
				%>
				<tr>
						<td height="30"></td>
						<td></td>
						<td></td>
						<td></td>
					<th>総注文数</th>
					<th>総売価合計</th>
				</tr>
				<tr>
					<td height="30"></td>
					<td></td>
					<td></td>
					<td></td>
					<td><%=nfNum.format(orderAll)%>個</td>
					<td><%=nfNum.format(priceAll)%>円</td>
				</tr>
				
			</table>
			<%}catch(SQLException e){
				e.printStackTrace();
			}
					%>
<%-- 			<% if(orderlist.size() == 0){ %>
 				<%
					while(s < max ) {
						out.print(1+ s/10);
						out.println("<a href='localhost:8080/itenVII/operator/orderList.jsp?start=" + s +"'>");
						s += 10;
					}
				%>
				<br />
				<br />
				<span>
					<a onClick="showButton('orderOpe');window.open('<%=request.getContextPath() %>/orderOpe/OrderListPrintServlet?
								<% for(j=0;j<orderlist.size();j++){Order order = orderlist.get(j);%>order_num=<%=order.getNum()%>
											<% if((j+1)<orderlist.size()){ %>&<%} %><%} %>','','width=400,height=500');return false;">
						<input type="button" value="印刷">
<!-- 					</a>
				</span>
 -->			<%
			}%>
 --%>			<a href ="/itenVII/operator/menu.jsp" name="戻る">メニューに戻る</a>
	</body>
</html>